package com.qf.homework.contoller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;

@RestController
public class AnalyzeController {

    @RequestMapping("/analyze")
    public HashMap<String,Object> analyze(HttpServletRequest request){

        HashMap<String, Object> resultMap = new HashMap<>();

        //1. 获取全部的请求参数
        HashMap<String, Object> paramMap = new HashMap<>();

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            // 获取参数的名字
            String parmaName = parameterNames.nextElement();
            // 获取参数的值
            String value = request.getParameter(parmaName);

            paramMap.put(parmaName,value);
        }

        resultMap.put("param",paramMap);

        //2. 获取全部的请求头
        HashMap<String, Object> headMap = new HashMap<>();

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            // 获取 头的名字
            String headName = headerNames.nextElement();
            // 获取 头的值
            String value = request.getHeader(headName);
            headMap.put(headName,value);
        }

        resultMap.put("heads",headMap);

        return resultMap;


    }

}
